package com.itheima.leetcode.od.c.binary;

import java.util.Arrays;
import java.util.Scanner;

/**
 * <h3>日志限流</h3>
 */
public class LogFlowRestriction {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[] records = new int[n];
        for (int i = 0; i < n; i++) {
            records[i] = sc.nextInt();
        }

        int total = sc.nextInt();

        System.out.println(getResult(n, records, total));
    }

    /**
     * 二分法
     *
     * @param n       系统某一天运行的单位时间数N
     * @param records 这一天每单位时间产生的日志数量的数组
     * @param total   系统一天可以保存的总日志条数total
     * @return 每单位时间内最大可保存的日志条数limit，如果不需要启动限流机制，返回-1
     */
    public static int getResult(int n, int[] records, int total) {
        int sum = Arrays.stream(records)
                .reduce(Integer::sum)
                .getAsInt();

        // 如果一天产生的日志条数总和小于等于total，则不需要启动限流机制．result为-1
        if (sum <= total) {
            return -1;
        }

        //    Arrays.sort(records);

        //    int max_limit = records[records.length - 1];
        int max_limit = Arrays.stream(records).max().getAsInt();
        int min_limit = total / n;

        while (min_limit <= max_limit) {
            int mid_limit = (max_limit + min_limit) >> 1;

            int tmp = 0;
            for (int record : records) {
                tmp += Math.min(record, mid_limit);
            }

            if (tmp <= total) {
                min_limit = mid_limit + 1;
            } else {
                max_limit = mid_limit - 1;
            }
        }

        return max_limit;
    }
}